<?php

namespace App\Services\Admin\Other;

use App\Exceptions\OtherException;
use Exception;

class ExpressService extends BaseOtherService
{

    /**
     * 物流详情
     * @param array $params
     * @return array
     * @throws OtherException
     * @throws Exception
     */
    public function info(array $params): array
    {
        if (empty($params['expressNo'])) {
            return [];
        }

        $data = $this->getExpressModel($params['type'])->getItemByExpressNo($params['expressNo'], [
            'id', 'express_company_code', 'express_no', 'phone'
        ]);
        if (empty($data)) {
            throw new OtherException('物流信息不存在', OtherException::CODE_1001);
        }

        // 查询物流信息
        try {
            $expressInfo = $this->getExpressApi()->query($data['express_company_code'], $data['express_no'], $data['phone']);
        } catch (Exception $exception) {
            throw new OtherException($exception->getMessage(), OtherException::CODE_1003);
        }

        if (!empty($expressInfo)) {
            // 更新物流状态
            $this->getExpressModel($params['type'])->updateItemById($data['id'], [
                'status' => 1
            ]);
        }

        return $expressInfo;
    }
}
